
package org.gaixie.micrite.crm.action;

import java.text.ParseException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.gaixie.micrite.action.GenericAction;
import org.gaixie.micrite.beans.Customer;
import org.gaixie.micrite.beans.CustomerSource;
import org.gaixie.micrite.crm.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;

public class CustomerAction extends GenericAction{ 
	private static final long serialVersionUID = 3072131320220662398L;

	@Autowired
	private ICustomerService customerService;

    private List<CustomerSource> customerSource;
    private Customer customer;
    private int[] customerIds;
    private Date startDate;
    private Date endDate;

    // ~~~~~~~~~~~~~~~~~~~~~~~  Action Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~//    
    public String add() {
        customer.setCreation_ts(new Date());
        customerService.add(customer);
        this.getResultMap().put("message", getText("save.success"));
        this.getResultMap().put("success", true);
        return SUCCESS;
    }
   public String advancedFind(){
        if (isFirstSearch()) {
           Integer count = customerService.advancedFindCount(getQueryBean());
            setTotalCount(count);
        }         
        List<Customer> customers = customerService.advancedFindByPerPage(getQueryBean(), this.getStart(), this.getLimit());
        this.putResultList(customers);
        return SUCCESS;
    }
   public String findByDateSpacing(){
        if (isFirstSearch()) {
           Integer count = customerService.findByCreateDateSpacingCount(startDate, endDate,customer.getCustomerSource().getId());
            setTotalCount(count);
        }         
       List<Customer> customers = customerService.findByCreateDateSpacingPerPage(startDate, endDate, this.getStart(), this.getLimit(),customer.getCustomerSource().getId());
        this.putResultList(customers);
        return SUCCESS;
    }
   public String update() {
        customerService.update(customer);
        this.getResultMap().put("message", getText("save.success"));
        this.getResultMap().put("success", true);
        return SUCCESS;
        
    }
   public String delete(){
        customerService.delete(customerIds);
        this.getResultMap().put("message", getText("delete.success"));
        this.getResultMap().put("success", true);
        return SUCCESS;
        
    }
   public String getPartner(){
        customerSource = customerService.findALLCustomerSource();
        return SUCCESS;
    }
  public String getPartnerByAll(){
        customerSource = customerService.findALLCustomerSource();
        CustomerSource tempSource = new CustomerSource();
        tempSource.setId(0);
        tempSource.setName("ALL");
        customerSource.add(tempSource);
       Collections.sort(customerSource, new Comparator<CustomerSource>(){
            public int compare(CustomerSource arg0, CustomerSource arg1){
                return arg0.getId().compareTo(arg1.getId());
            }
        });

        return SUCCESS;
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~  Accessor Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~//    
    /**
     * @return the customer
     */
    public Customer getCustomer() {
        return customer;
    }
    /**
     * @param customer the customer to set
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    /**
     * @return the customerSource
     */
    public List<CustomerSource> getCustomerSource() {
        return customerSource;
    }

    /**
     * @param customerSource the customerSource to set
     */
    public void setCustomerSource(List<CustomerSource> customerSource) {
        this.customerSource = customerSource;
    }
    public int[] getCustomerIds() {
        return customerIds;
    }
    public void setCustomerIds(int[] customerIds) {
        this.customerIds = customerIds;
    }
    public void setStartDate(String startDate) throws ParseException {
        this.startDate = DateUtils.parseDate(startDate + ":00", new String[]{"yyyy-MM-dd hh:mm:ss"});
    }
    public void setEndDate(String endDate) throws ParseException {
        this.endDate = DateUtils.parseDate(endDate + ":00", new String[]{"yyyy-MM-dd hh:mm:ss"});
    }
}
